﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.WarehouseInsp.Buz.DataAccess;
using Microhard.WarehouseInsp.Buz.BuzEnum;
using Microhard.WarehouseInsp.Buz.Msg;
using Microhard.Core;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.DTO;
using Microhard.WarehouseInsp.Buz.JHWorkflow;

namespace Microhard.WarehouseInsp.Buz.Impls
{
    public class ChangePlgImpl : IChangePlgPrice
    {
        public ExecutedResult Add(PledgeChangeRecordModel model)
        {
            throw new NotImplementedException();
        }

        public ExecutedResult Update(PledgeChangeRecordModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            //获得质押物当前货值
            PledgeChangeRecord entity = db.PledgeChangeRecords.SingleOrDefault(w => w.PledgeProductID
                == model.PledgeProductID);
            //保存历史数据
            entity.PreviousAmouont = entity.CurrentAmount;
            entity.PreviousPrice = entity.CurrentPrice;
            entity.UpdateTime = DateTime.Now;
            //更新
            entity.CurrentAmount = model.CurrentAmount;
            entity.CurrentPrice = model.CurrentPrice;

            entity.RecordType = (int)RecordType.ChangePrice;
            entity.CreateTime = DateTime.Now;
            entity.OperatorID = this.scope.CurrentUserID;
            entity.OperatorName = this.scope.CurrentUserRealName;

            try
            {
                db.SaveChanges();
                //发送提醒
                var prj = db.SupProjects.SingleOrDefault(p => p.ProjectID == entity.PledgePlanInRecord.ProjectID);
                StringBuilder content = new StringBuilder();
                content.Append("质押物调价通知");
                content.Append(",项目:" + prj.Name);
                content.Append(",质押物:" + entity.PledgePlanInRecord.ProductList.Name);
                content.Append(",类别:" + entity.PledgePlanInRecord.PlgCategory);
                content.Append(",质押物调整前单价:" + entity.PreviousPrice);
                content.Append(",质押物调整后单价:" + entity.CurrentPrice);
                content.Append(",质押物调整前总价:" + entity.PreviousAmouont);
                content.Append(",质押物调整后总价:" + entity.CurrentAmount);
                content.Append(",提交人:" + this.scope.CurrentUserRealName + ",请注意!");
                SendAlertByJH.SendAlert(prj.ProjectID, content.ToString());
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, ChangePlgPriceMsg.AddFailed);
                Log.Error("ChangePlgImpl.Add", ex);
            }
            return rtn;
        }

        public ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public ExecutedResult Delete(PledgeChangeRecordModel model)
        {
            throw new NotImplementedException();
        }

        public PledgeChangeRecordModel GetModelByID(Guid id)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            var entity = db.PledgeChangeRecords.SingleOrDefault(e => e.PledgeProductID == id);
            if (entity != null)
            {
                var data = entity.ToModel();
                return data;
            }
            return null;
        }

        public PledgeChangeRecordModel GetModelByModel(PledgeChangeRecordModel model)
        {
            throw new NotImplementedException();
        }

        public List<PledgeChangeRecordModel> Query(PledgeChangeRecordModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            IEnumerable<PledgeChangeRecord> entities = from b in db.PledgeChangeRecords
                                                       where b.PledgePlanInRecord.SupProject.PMID ==
                                                       this.scope.CurrentUserID
                                                       && b.IsUsing == true
                                                       select b;

            totalCount = entities.Count();
            var pagedEntities = entities.OrderBy(p => p.CreateTime).ToList();

            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();

            return new List<PledgeChangeRecordModel>();
        }

        public List<PledgeChangeRecordModel> Query(PledgeChangeRecordModel Model)
        {
            throw new NotImplementedException();
        }

        public ScopeResult scope
        {
            set;
            get;
        }
    }
}
